﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core;
using Medianamik.Core.Querying;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.Controls
{
    public class ChildrenDropDownList : DropDownList
    {
        protected override void OrderItems(ref IList<Node> items)
        {
            items = items.OrderBy(i => i.Name).ToList();
        }

        protected override IList<Node> GetOptionNodes(IEnumerable<Guid> options)
        {
            if (options.Count() == 0 || CurrentNode == null ||
                !CurrentNode.HasChildren || CurrentNode.Status == EntityStates.New)
                return Enumerable.Empty<Node>().ToList();

            var query = new Query(false);

            query.Where(n => n.Path, Comparison.Like, CurrentNode.Path + DataUtil.GetSQLInclusivityOperator(false))
                .Where(n => n.Depth, Comparison.MoreThan, CurrentNode.Depth);

            if (options.Count() == 1)
                query.Where(n => n.ID, Comparison.Equal, options.First());
            else
                query.Where(n => n.ID, Comparison.In, options);

            return query.Execute().ToList();
        }
    }
}
